/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.tsunami.plugins.detectors.directorytraversal.genericpathtraversaldetector;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.tsunami.common.net.http.HttpRequest;
import com.google.tsunami.proto.NetworkService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class ExploitGeneratorTest {
  private static final NetworkService MINIMAL_NETWORK_SERVICE =
      NetworkService.newBuilder().setServiceName("http").build();
  private static final String PAYLOAD = "../../../../etc/passwd";

  private static final HttpRequest BASIC_HTTP_REQUEST =
      HttpRequest.get("https://google.com").withEmptyHeaders().build();

  @Test
  public void injectPayload_always_generatesPotentialExploits() {
    InjectionPoint mockInjectionPoint = mock(InjectionPoint.class);
    PotentialExploit exploit =
        PotentialExploit.create(
            MINIMAL_NETWORK_SERVICE,
            HttpRequest.get("https://google.com/../../../../etc/passwd").withEmptyHeaders().build(),
            PAYLOAD,
            PotentialExploit.Priority.LOW);
    when(mockInjectionPoint.injectPayload(MINIMAL_NETWORK_SERVICE, BASIC_HTTP_REQUEST, PAYLOAD))
        .thenReturn(ImmutableList.of(exploit));

    ExploitGenerator exploitGenerator =
        new ExploitGenerator(
            BASIC_HTTP_REQUEST, MINIMAL_NETWORK_SERVICE, ImmutableSet.of(mockInjectionPoint));

    assertThat(exploitGenerator.injectPayload(PAYLOAD)).containsExactly(exploit);
  }

  @Test
  public void injectPayload_givenNoInjectionPoint_returnsEmpty() {
    ExploitGenerator exploitGenerator =
        new ExploitGenerator(BASIC_HTTP_REQUEST, MINIMAL_NETWORK_SERVICE, ImmutableSet.of());

    assertThat(exploitGenerator.injectPayload(PAYLOAD)).isEmpty();
  }

  @Test
  public void injectPayload_givenMultipleInjectionPoints_generatesAllPotentialExploits() {
    InjectionPoint mockPathInjectionPoint = mock(InjectionPoint.class);
    PotentialExploit pathExploit =
        PotentialExploit.create(
            MINIMAL_NETWORK_SERVICE,
            HttpRequest.get("https://google.com/../../../../etc/passwd").withEmptyHeaders().build(),
            PAYLOAD,
            PotentialExploit.Priority.LOW);
    when(mockPathInjectionPoint.injectPayload(MINIMAL_NETWORK_SERVICE, BASIC_HTTP_REQUEST, PAYLOAD))
        .thenReturn(ImmutableList.of(pathExploit));
    InjectionPoint mockGetParameterInjectionPoint = mock(InjectionPoint.class);
    PotentialExploit getParameterExploit =
        PotentialExploit.create(
            MINIMAL_NETWORK_SERVICE,
            HttpRequest.get("https://google.com/?key=../../../..//etc/passwd")
                .withEmptyHeaders()
                .build(),
            PAYLOAD,
            PotentialExploit.Priority.LOW);
    when(mockGetParameterInjectionPoint.injectPayload(
            MINIMAL_NETWORK_SERVICE, BASIC_HTTP_REQUEST, PAYLOAD))
        .thenReturn(ImmutableList.of(getParameterExploit));

    ExploitGenerator exploitGenerator =
        new ExploitGenerator(
            BASIC_HTTP_REQUEST,
            MINIMAL_NETWORK_SERVICE,
            ImmutableSet.of(mockPathInjectionPoint, mockGetParameterInjectionPoint));

    assertThat(exploitGenerator.injectPayload(PAYLOAD))
        .containsExactly(pathExploit, getParameterExploit);
  }

  @Test
  public void toString_always_includesInformationAboutRequest() {
    HttpRequest getRequest =
        HttpRequest.get("https://google.com/path/to/file").withEmptyHeaders().build();
    ExploitGenerator exploitGenerator =
        new ExploitGenerator(getRequest, MINIMAL_NETWORK_SERVICE, ImmutableSet.of());

    assertThat(exploitGenerator.toString()).contains(getRequest.toString());
  }
}
